//
//  WelcomViewController.m
//  BlocksKit
//
//  Created by admin on 2020/4/14.
//

#import "WelcomViewController.h"
#import <SVGAPlayer/SVGA.h>
#import <SDWebImage/SDWebImage.h>
#import <SuperPlayer/SuperPlayer.h>
#import <TXLiteAVSDK_Player/TXVodPlayer.h>

@implementation WelcomItem
@end

@interface WelcomViewController ()<TXVodPlayListener,SVGAPlayerDelegate>


@property(strong, nonatomic) UIImageView *imageView;
@property(strong, nonatomic) SVGAPlayer *svgaPlayer;
@property(strong, nonatomic) TXVodPlayer *superPlayer;

@end

@implementation WelcomViewController

-(void)loadView{
    
    [super loadView];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self configUI];
    [self loadSorce];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.view.alpha = 0.1;
    [UIView animateWithDuration:0.75 animations:^{
        self.view.alpha = 1;
    }];
}
-(void)loadSorce{
    if (!_item) {
        NSInteger i = random()%3;
        //测试
        _item = [WelcomItem new];
        switch (i) {
            case 0:
            {   _item.type = 0;
                _item.resUrl = UGURL(@"http://seopic.699pic.com/photo/50109/8797.jpg_wh1200.jpg");
                
            }
                break;
            case 1:
            {
                _item.type = 1;
                _item.resName = @"rose_2.0.0";
            }
                break;
            case 2:
            {
                _item.type = 2;
                _item.resUrl = UGURL(@"https://i1.ygimg.cn/pics/shop/cms/advertising_video/advertising_video__joypeace_2019.mp4");
            }
                break;
                
            default:
                break;
        }
  
    }
    switch (_item.type) {
        case 0:{
            [self.view insertSubview:self.imageView atIndex:0];
    
            [self.imageView sd_setImageWithURL:_item.resUrl];
            UG_WEAKSELF
            if (@available(iOS 10.0, *)) {
                [NSTimer scheduledTimerWithTimeInterval:8 repeats:NO block:^(NSTimer * _Nonnull timer) {
                    if (weakSelf.endBlock) {
                        weakSelf.endBlock(self);
                    }
                }];
            } else {
                // Fallback on earlier versions
            }
        }
            break;
        case 1:{
            UG_WEAKSELF
            [self.view insertSubview:self.svgaPlayer atIndex:0];
        
            SVGAParser *parser = [[SVGAParser alloc] init];
            if (_item.resUrl) {
                [parser parseWithURL:_item.resUrl completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
                    if (videoItem != nil) {
                        weakSelf.svgaPlayer.videoItem = videoItem;
                        [weakSelf.svgaPlayer startAnimation];
                    }
                } failureBlock:^(NSError * _Nullable error) {
                    ;
                }];
            }else{
                [parser parseWithNamed:_item.resName inBundle:_item.resPath completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
                    if (videoItem != nil) {
                        weakSelf.svgaPlayer.videoItem = videoItem;
                        [weakSelf.svgaPlayer startAnimation];
                    }
                } failureBlock:^(NSError * _Nonnull error) {
                    
                }];
            }
        }
            break;
        case 2:{

           [self.superPlayer setupVideoWidget:self.view insertIndex:0];
            [_superPlayer startPlay:_item.resUrl.absoluteString];
        }
            break;
            
        default:
            break;
    }
}

-(void)configUI{
    self.view.backgroundColor = COLORPRIMARY;
    [self.view addSubview:self.closeBtn];
}

-(UIButton *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [UIButton new];
        [_closeBtn setTitle:@"跳过" forState:UIControlStateNormal];
        [_closeBtn.titleLabel setFont:FONT_SYS12];
        [_closeBtn ug_borderColor:COLORPRIMARY width:1];
        [_closeBtn ug_radius:22];
        [_closeBtn ug_shadowColor:COLORINFO width:4];
    }
    return _closeBtn;
}
-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [UIImageView new];
    }
    return _imageView;
}

-(SVGAPlayer*)svgaPlayer{
    if(!_svgaPlayer){
        _svgaPlayer = [SVGAPlayer new];
        _svgaPlayer.delegate = self;
        _svgaPlayer.loops = 1;
        _svgaPlayer.clearsAfterStop = NO;
    }
    return _svgaPlayer;
}

-(TXVodPlayer *)superPlayer{
    if(!_superPlayer){
        _superPlayer = [TXVodPlayer new];
        _superPlayer.vodDelegate = self;
    }
    return _superPlayer;
}
#pragma mark suplePlayer
-(void) onPlayEvent:(TXVodPlayer *)player event:(int)EvtID withParam:(NSDictionary*)param
{
    UG_WEAKSELF
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (EvtID == PLAY_EVT_PLAY_END) {
            if (weakSelf.endBlock) {
                weakSelf.endBlock(self);
            }
        }
    });
}
 /*
  网络状态通知
 */
-(void) onNetStatus:(TXVodPlayer *)player withParam:(NSDictionary*)param{
    
}
#pragma mark svga

-(void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player{
    if (_endBlock) {
        _endBlock(self);
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_superPlayer stopPlay];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view).mas_offset(-10);
        make.top.mas_equalTo(self.view).mas_offset(10+KSAFE_Top);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    [_svgaPlayer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
@end
